extends "res://BaseEnemy.gd"

# 沙漠史莱姆特有属性
@export var split_on_death = false  # 是否在死亡时分裂
@export var split_count = 2  # 分裂数量
@export var split_size_factor = 0.7  # 分裂后的大小因子

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置沙漠史莱姆特有属性
	max_health = 25
	current_health = max_health
	movement_speed = 80.0
	damage = 8
	experience_value = 4
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(0.9, 0.8, 0.6)  # 沙漠色调

func die():
	# 如果需要分裂且不是由分裂产生的小史莱姆（通过大小判断）
	if split_on_death and sprite and sprite.scale.x > 0.5:
		# 生成分裂的小史莱姆
		for i in range(split_count):
			var small_slime = load("res://DesertSlime.tscn").instantiate()
			get_tree().get_root().add_child(small_slime)
			small_slime.global_position = global_position + Vector2(randf_range(-20, 20), randf_range(-20, 20))
			
			# 设置小史莱姆属性
			small_slime.max_health = int(max_health * 0.5)
			small_slime.current_health = small_slime.max_health
			small_slime.movement_speed = movement_speed * 1.2
			small_slime.damage = int(damage * 0.7)
			small_slime.experience_value = int(experience_value * 0.5)
			
			# 设置小史莱姆大小
			if small_slime.sprite:
				small_slime.sprite.scale = sprite.scale * split_size_factor
			
			# 禁止小史莱姆再次分裂
			small_slime.split_on_death = false
	
	# 调用父类的die方法
	super.die()

# 沙漠史莱姆特有的跳跃攻击
func attack_player():
	if animation_player and not animation_player.is_playing():
		if animation_player.has_animation("jump_attack"):
			animation_player.play("jump_attack")
			
			# 跳向玩家
			if target_player:
				var jump_direction = (target_player.global_position - global_position).normalized()
				velocity = jump_direction * movement_speed * 2
				
				# 等待一小段时间后造成伤害
				await get_tree().create_timer(0.3).timeout
				
				# 如果跳跃后接近玩家，造成伤害
				if target_player and is_instance_valid(target_player):
					if global_position.distance_to(target_player.global_position) <= attack_range * 1.5:
						target_player.take_damage(damage)
		else:
			# 如果没有跳跃攻击动画，使用普通攻击
			super.attack_player()
